<html>
<head>
<meta http-equiv="content-type" content="text-html; charset=utf-8">
<script type="text/javascript" src="helper.js"></script>
</head>
<body>
<script type="text/javascript">

// Callback after the phish check is done.
function onChkPhishDone(checkRst,tab) {
	if (checkRst.success == 1) {
		switch (checkRst.phish) {
			case 0: //Not a phish
				chrome.pageAction.setIcon({tabId: tab.id, path: 'icon.png'});
				break;
			case -1: // Unknown
				chrome.pageAction.setIcon({tabId: tab.id, path: 'icon.png'});
				break;
			case 1: // phish
				chrome.pageAction.setIcon({tabId: tab.id, path: 'icon.png'});
				break;
			case 2: // High Rish
				chrome.pageAction.setIcon({tabId: tab.id, path: 'icon.png'});
				break;
			default:
				console.log("Unexpected ret value : " + checkRst.phish);
		}
	} else {
		// Request failure, hide the icon
		
	}
};

chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
		console.log("Updated : " + new Date().getTime());
		console.log(changeInfo);
		console.log(tab);
		if(changeInfo.status == 'loading' && (changeInfo.url.substr(0,7) == 'http:\/\/'
		|| changeInfo.url.substr(0,8) == 'https:\/\/')) {
			chrome.pageAction.setIcon({'tabId': tabId, path: 'icon_cloud64.png'});
			chrome.pageAction.show(tabId);
			Kscloudsec.checkPhish(changeInfo.url, onChkPhishDone,tab);
		}
  }
 );


// Callback after the download check is done.
function onChkDownloadDone(checkRst,tab) {
	if (checkRst.success == 1) {
		switch (checkRst.down_type) {
			case 1: // unkonwn
				break;
			case 2: // secure
				break;
			case 3: // Dangerous!
				break;
			case 6: // Non PE file
				break;
			default:
				console.log("Unexpected down_type : " + checkRst.down_type);		
		}
			 
		console.log("Download ok!");
		console.log(checkRst);
	} else {
	
	}

};

function uiCheckAllLinks(info, tab) {
	var links = [];
	for (var idx in links) {
		Kscloudsec.checkDownload(links[idx],onChkDownloadDone, tab);
	}
};

function uiCheckSingleLink(info, tab) {
	url = info.linkUrl;
	Kscloudsec.checkDownload(url,onChkDownloadDone,tab);
};

chrome.contextMenus.create( {
							title : '检查本页所有链接',
							contexts : ['page'],
							onclick : uiCheckAllLinks
							});

chrome.contextMenus.create( {
							title : '检查此链接的安全性',
							contexts : ['link'],
							onclick : uiCheckSingleLink
							});
</script>
</body>
</html>